_import("aiviki.event");
_import("aiviki.util");
aiviki.gi.cchess = aiviki.Class.create("aiviki.gi.cchess");
aiviki.gi.cchess.prototype = 
	{
	init:function(_a)
		{
		this.renderDiv = _a;
		this.renderInit();
		this.ych=new Array();
		this.hch=new Array();
		this.initGame();
		},
	rednerDiv:null,
	status:0,
	ych:null,
	hch:null,
	enabled:true,
	you:0,
	ched:null,
	chedIndex:null,
	rule:-1,
	moveO:null,
	getEventDiv:function(e)
		{
		var o =e.srcElement;
			if(o.tagName=="DIV")return o.parentNode;
			if(o.tagName=="TD")return o;
			if(o.tagName=="SPAN")return o.parentNode.parentNode;
		return null;
		},
	renderInit:function()
		{
			var td,tr;
			var gtable = document.createElement("table");
			var gbody = document.createElement("TBODY");
			gtable.setAttribute("border",0);
			gtable.setAttribute("cellpadding",0);
			gtable.setAttribute("cellspacing",0);
			gtable.setAttribute("width",171);
			gtable.className="cchess";
			gtable.setAttribute("height",190);

			gtable.appendChild(gbody);
			for(var y = 0 ;y<10;y++)
				{
				tr=document.createElement("tr");
				gbody.appendChild(tr);
				for(var x = 0 ;x<9;x++)
					{
					td= document.createElement("td");
					tr.appendChild(td);
					td.style.MozUserSelect="none";
					td.id="c_"+x+"_"+y;
					td.className="mu";
					td.setAttribute("width",17);
					td.setAttribute("height",17);
					div = document.createElement("div");
					div.className="";
					div.x=x;
					div.y=y;
					td.appendChild(div);
					div.id=this.UIID+"_c_d"+x+"_"+y;
					span = document.createElement("span");
					span.id=this.UIID+"_c_s"+x+"_"+y;
					span.className='';
					span.innerHTML="&nbsp;&nbsp;&nbsp;";
					
					div.appendChild(span);
					aiviki.event.addEventByObj(this.UIID+"_c_a",td,"click",this.cClick,this);
					aiviki.event.addEventByObj(this.UIID+"_c_a",td,"mousemove",this.cMove,this);
					aiviki.event.addEventByObj(this.UIID+"_c_a",td,"mouseout",this.cOut,this);

					}
				}
			this.renderDiv.appendChild(gtable);
		},
		initGame:function()
		{
			var tmpx = new Array(0,1,2,3,4,5,6,7,8,0,2,4,6,8,1,7);
			var tmpy = new Array(9,9,9,9,9,9,9,9,9,6,6,6,6,6,7,7);
			var tmpw = new Array(3,4,2,1,0,1,2,4,3,6,6,6,6,6,5,5);
			if(this.you==0)
			{
				var yc = "bu";
				var hc = "re";
			
			}
			else
			{
				var hc = "bu";
				var yc = "re";
			}
				
			for(var i=0;i<tmpx.length;i++)
			{
			var o = new Object();
			o.yh=true;
			o.x=tmpx[i];
			o.y=tmpy[i];
			o.obj=$bi(this.UIID+"_c_d"+tmpx[i]+"_"+tmpy[i]);
			o.cn = "y"+i;
			o.lv = tmpw[i];	
			this.ych["y"+i] = o;
			this.setCh(o,yc);

			}

			for(var i=0;i<tmpx.length;i++)
			{
			ci = Math.abs(tmpy[i]-9);
			var o = new Object();
			o.yh=false;
			o.x=tmpx[i];
			o.y=ci;
			o.obj=$bi(this.UIID+"_c_d"+tmpx[i]+"_"+ci);
			o.cn = "h"+i;
			o.lv = tmpw[i];	
			this.hch["h"+i] = o;
			this.setCh(o,hc);

			}
		},
		setCh:function(co,hc)
		{
			var o = $bi(this.UIID+"_c_d"+co.x+"_"+co.y);
			o.className=hc;
			o.ab = co.lv;
			o.yh = co.yh;
			o.cn = co.cn;
			$bi(this.UIID+"_c_s"+co.x+"_"+co.y).className=hc+co.lv;

		},
		removeCh:function(x,y)
		{
			var o = $bi(this.UIID+"_c_d"+x+"_"+y);
			o.className="";
			o.ab = null;
			o.yh =null;
			o.cn = null;
			$bi(this.UIID+"_c_s"+x+"_"+y).className="";
			o.parentNode.className="";
		},
		checkMove:function(o)
		{
			var yo = this.ched;
			switch(yo.lv)
			{
				case 0:
				{
				if(yo.yh)
					{
					if((o.y==yo.y-1&&o.x==yo.x)||(o.y==yo.y+1&&o.x==yo.x)||(o.x==yo.x-1&&o.y==yo.y)||(o.x==yo.x+1&&o.y==yo.y))
						{
						if($bi(this.UIID+"_c_d"+o.x+"_"+o.y).cn==null)
							{
							return true;
							}else{
							if($bi(this.UIID+"_c_d"+o.x+"_"+o.y).yh==false)
								return true;
							}
						
						}
							return false;
					}else{
					}
				break;
				}
				case 1:
				{
				if(yo.yh)
					{
					if((o.y==yo.y+1&&o.x==yo.x+1)||(o.y==yo.y-1&&o.x==yo.x-1)||(o.y==yo.y-1&&o.x==yo.x+1)||(o.y==yo.y+1&&o.x==yo.x-1))
						{
						if($bi(this.UIID+"_c_d"+o.x+"_"+o.y).cn==null)
							{
							return true;
							}else{
							if($bi(this.UIID+"_c_d"+o.x+"_"+o.y).yh==false)
								return true;
							}
						
						}
							return false;
					}else{
					}

				break;
				}
				case 2:
				{
				if(yo.yh)
					{
					if((o.y==yo.y+2&&o.x==yo.x+2)||(o.y==yo.y-2&&o.x==yo.x-2)||(o.y==yo.y-2&&o.x==yo.x+2)||(o.y==yo.y+2&&o.x==yo.x-2))
						{
						if($bi(this.UIID+"_c_d"+o.x+"_"+o.y).cn==null)
							{
							return true;
							}else{
							if($bi(this.UIID+"_c_d"+o.x+"_"+o.y).yh==false)
								return true;
							}
						
						}
							return false;
					}else{
					}
				break;
				}				
				case 3:
				{
				if(yo.yh)
					{
					if((o.y>yo.y&&o.x==yo.x)||(o.y<yo.y&&o.x==yo.x)||(o.y==yo.y&&o.x<yo.x)||(o.y=yo.y&&o.x>yo.x))
						{
						if($bi(this.UIID+"_c_d"+o.x+"_"+o.y).cn==null)
							{
							return true;
							}else{
							if($bi(this.UIID+"_c_d"+o.x+"_"+o.y).yh==false)
								return true;
							}
						
						}
							return false;
					}else{
					}

				break;
				}
				case 4:
				{
				if(yo.yh)
					{
					if((o.y==yo.y+2&&o.x==yo.x+1)||(o.y==yo.y+2&&o.x==yo.x-1)||(o.y==yo.y-2&&o.x==yo.x-1)||(o.y==yo.y-2&&o.x==yo.x+1)||(o.y==yo.y-1&&o.x==yo.x+2)||(o.y==yo.y-1&&o.x==yo.x-2)||(o.y==yo.y+1&&o.x==yo.x-2)||(o.y==yo.y+1&&o.x==yo.x+2))
						{
						if($bi(this.UIID+"_c_d"+o.x+"_"+o.y).cn==null)
							{
							return true;
							}else{
							if($bi(this.UIID+"_c_d"+o.x+"_"+o.y).yh==false)
								return true;
							}
						
						}
							return false;
					}else{
					}

				break;
				}
				case 5:
				{
				break;
				}
				case 6:
				{
				if(yo.yh)
					{
					if((o.y==yo.y-1&&o.x==yo.x)||(o.y==yo.y+1&&o.x==yo.x)||(o.x==yo.x-1&&o.y==yo.y)||(o.x==yo.x+1&&o.y==yo.y))
						{
						if($bi(this.UIID+"_c_d"+o.x+"_"+o.y).cn==null)
							{
							return true;
							}else{
							if($bi(this.UIID+"_c_d"+o.x+"_"+o.y).yh==false)
								return true;
							}
						
						}
							return false;
					}else{
					}
				break;
				}	
			}
			return false;
		},
		cClick:function(e)
		{
			var o =this.getEventDiv(e);
			if(!o.childNodes[0].yh&&this.moveO==null)return;;
			if(this.ched!=null)
			{
				if(this.ched.cn==o.childNodes[0].cn)
				{
					this.ched=null;
					
				}else{
				if(this.checkMove(o.childNodes[0]))
					{

					if($bi(this.UIID+"_c_d"+o.childNodes[0].x+"_"+o.childNodes[0].y).cn==null)
						{

						this.removeCh(this.ched.x,this.ched.y);
						this.ched.x= o.childNodes[0].x;
						this.ched.y =o.childNodes[0].y;
						this.setCh(this.ched,"bu");
						this.ched=null;
						}else{
						if($bi(this.UIID+"_c_d"+o.childNodes[0].x+"_"+o.childNodes[0].y).yh)return;
						}
					}else{
					this.ched.obj.parentNode.className="mu";
					this.ched = this.ych[o.childNodes[0].cn];
					}

				}
			}else{
					this.ched = this.ych[o.childNodes[0].cn];

			}
		},
		cMove:function(e)
		{
		var o =this.getEventDiv(e);
		if(!o.childNodes[0].yh&&this.ched==null)return;
		if(this.ched!=null)
			{
			if(!this.checkMove(o.childNodes[0]))return;
				o.className="mo";
				this.moveO=o;
			}else{
			o.className="mo";
			}
	
		},
		cOut:function(e)
		{
		var o =this.getEventDiv(e);
		if(this.moveO!=null)
			{
			this.moveO.className="mu";
			this.moveO=null;
			return;
			}
		if(!o.childNodes[0].yh)return;;
		if(this.ched==null)
			{
			o.className="mu";
			}else{
			if(o.childNodes[0].cn ==this.ched.cn)return;
			o.className="mu"
			}
		}
	}
